package com.tfswue.lh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tfswue.lh.entity.Book;
import com.tfswue.lh.mapper.BookMapper;
import com.tfswue.lh.service.Bookservice;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookserviceImpl extends ServiceImpl<BookMapper, Book> implements Bookservice {
    
    /**
     * 根据ID获取图书（带缓存）
     * @param id 图书ID
     * @return 图书信息
     */
    @Override
    @Cacheable(
            value = "dataCache",
            key = "'book:' + #id",
            unless = "#result == null"
    )
    public Book getById(java.io.Serializable id) {
        return super.getById(id);
    }
    
    /**
     * 更新图书（更新缓存）
     * @param entity 图书实体
     * @return 是否成功
     */
    @Override
    @CachePut(
            value = "dataCache",
            key = "'book:' + #entity.id"
    )
    public boolean updateById(Book entity) {
        return super.updateById(entity);
    }
    
    /**
     * 删除图书（清除缓存）
     * @param id 图书ID
     * @return 是否成功
     */
    @Override
    @CacheEvict(
            value = "dataCache",
            key = "'book:' + #id"
    )
    public boolean removeById(java.io.Serializable id) {
        return super.removeById(id);
    }
}
